package cn.tedu.ykmall.admin.commodity.webapi.service;

import cn.tedu.ykmall.admin.commodity.service.ICategoryService;
import cn.tedu.ykmall.admin.commodity.webapi.mapper.CategoryMapper;
import cn.tedu.ykmall.pojo.commodity.dto.AdminCategoryByDepthDTO;
import cn.tedu.ykmall.pojo.commodity.dto.AdminCategoryByParentIdDTO;
import cn.tedu.ykmall.pojo.commodity.entity.Category;
import cn.tedu.ykmall.pojo.commodity.vo.AdminCategoryTwoByDepthVO;
import cn.tedu.ykmall.pojo.commodity.vo.CategoryVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CategoryService implements ICategoryService {
    @Autowired
    private CategoryMapper mapper;

    @Override
    public void insertOneCategory(CategoryVO categoryVO) {
        Category category = new Category();
        //设置输入的值
        log.info("传入的名称为：{}",categoryVO.getName());
        category.setName(categoryVO.getName());
        category.setParentId(0L);
        category.setDepth(1);
        category.setEnable(1);
        category.setIsParent(1);
        category.setIsDisplay(0);
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        category.setGmtCreate(now);
        log.warn("插入的类别表属性为：{}",category.toString());
        mapper.insertCategory(category);
    }

    @Override
    public List<AdminCategoryByDepthDTO> selectCategoryByDepth(Integer depth) {
        List<AdminCategoryByDepthDTO> dtoList = mapper.selectCategoryByDepth(depth);
        return dtoList;
    }

    @Transactional
    @Override
    public List<AdminCategoryTwoByDepthVO> selectCategoryByParentId() {
        List<AdminCategoryByDepthDTO> lists = selectCategoryByDepth(2);
        System.out.println("查询的数据为："+lists);
        List<AdminCategoryTwoByDepthVO> voList = new ArrayList<>();
        AdminCategoryTwoByDepthVO twoByDepthVO = null;
        for(AdminCategoryByDepthDTO list : lists){
            twoByDepthVO = new AdminCategoryTwoByDepthVO();
            Long id = list.getId();
            twoByDepthVO.setId(id);
            twoByDepthVO.setTwoName(String.valueOf(list.getName()));
            Integer parentId = list.getParentId();
            twoByDepthVO.setOneName(mapper.selectCategoryByParentId(parentId).getName());
            log.warn("添加的数据为:"+twoByDepthVO);
            voList.add(twoByDepthVO);
        }
        return voList;
    }
}
